$(function () {

    let val = "";
    for (let i = 0; i < $(".label-selected").children("li").length; i++) {
        val += $(".label-selected").children("li").eq(i).attr("data") + ',';
    }
    $("input[name='label']").val(val);

    $(".show-labelitem").on("click", function () {
        $(this).hide();
        $(".hide-labelitem").show();
        $("#labelItem").show();
    });
    $(".hide-labelitem").on("click", function () {
        $(this).hide();
        $(".show-labelitem").show();
        $("#labelItem").hide();
    });
    $(".label-item").on("click", "li", function () {
        let id = $(this).attr("data");
        let text = $(this).children("span").html();
        let labelHTML = "<li data='" + id + "''> " + text + "</li>";
        if ($(this).hasClass("selected")) {
            return false;
        } else if ($(".label-selected").children("li").length >= 5) {
            layer.msg("最多可以选择5个哦");
            return false;
        }
        $(".label-selected").append(labelHTML);
        val = '';
        for (let i = 0; i < $(".label-selected").children("li").length; i++) {
            val += $(".label-selected").children("li").eq(i).attr("data") + ',';
        }
        $("input[name='label']").val(val);
        $(this).addClass("selected");
    });

    $(".label-selected").on("click", "li", function () {
        let id = $(this).attr("data");
        val = '';
        $(this).remove();
        for (let i = 0; i < $(".label-selected").children("li").length; i++) {
            val += $(".label-selected").children("li").eq(i).attr("data") + ',';
        }
        $("input[name='label']").val(val);
        $(".label-item").find("li[data='" + id + "']").removeClass("selected");
    });
});


let duty;
let requirement;
layui.use('layedit', function () {
    let layedit = layui.layedit;
    duty = layedit.build('duty', {
        height: 150,
        tool: ['strong', 'italic', 'underline', '|', 'left', 'center', 'right']
    }); //建立编辑器
    requirement = layedit.build('requirement', {
        height: 150,
        tool: ['strong', 'italic', 'underline', '|', 'left', 'center', 'right']
    }); //建立编辑器

    //编辑器外部操作
    let active = {
        send: function () {
            layer.confirm('请确认是否修改', {
                btn: ['确认', '取消']
                , yes: function (index) {
                    layer.close(index);
                    // 获取表单的值
                    // 获取当前页面的数据
                    let param = $("#edit_form").serialize();

                    let dutyContent = layedit.getContent(duty);
                    dutyContent = dutyContent.replace(new RegExp('&nbsp;', 'g'), ' ');
                    param = param + "&duty=" + dutyContent;

                    let requirementContent = layedit.getContent(requirement);
                    requirementContent = requirementContent.replace(new RegExp('&nbsp;', 'g'), ' ');
                    param = param + "&requirement=" + requirementContent;

                    let tags = $("input[name='label']").val();
                    console.log("tag:" + tags);
                    param = param + "&tags=" + tags;

                    console.log(param);

                    $.post('/hire/employer/recruitment/update', param, function (res) {
                        console.log(res);
                        if (res.code == '200') {
                            window.location.href = "/hire/employer/view?id=" + res.data;
                        } else {
                            layer.msg(res.msg);
                            if (res.code == '500') {
                                layer.tips(res.msg, '#name');
                            } else if (res.code == '501') {
                                layer.tips(res.msg, '#salaryUpper');
                            } else if (res.code == '502') {
                                layer.tips(res.msg, '#contact_name');
                            } else if (res.code == '503') {
                                layer.tips(res.msg, '#contact_phone');
                            } else if (res.code == '504') {
                                layer.tips(res.msg, '#email');
                            }
                        }

                    });
                }
            });
        },
        cancel: function () {
            let recruitmentId = $("#recruitment").val();
            window.location.href = "/hire/employer/view?id="+recruitmentId;

        }
    };

    $('#send').on('click', function () {
        active["send"].call(this);
    });
    $("#cancel").on('click', function () {
        active["cancel"].call(this);
    })

});